<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="部门ID" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="焊接规则" prop="weldingSequenceRule">
        <el-input
          v-model="queryParams.weldingSequenceRule"
          placeholder="请输入焊接规则"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优先级" prop="weldingSequencePriority">
<!--        <el-input-->
<!--          v-model="queryParams.weldingSequencePriority"-->
<!--          placeholder="请输入焊接顺序优先级"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.weldingSequencePriority" placeholder="请选择焊接顺序优先级" clearable>
          <el-option label="高" value="高"></el-option>
          <el-option label="中" value="中"></el-option>
          <el-option label="低" value="低"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选中状态" prop="selectedState">
<!--        <el-input-->
<!--          v-model="queryParams.selectedState"-->
<!--          placeholder="请输入选中状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.selectedState" placeholder="请选择选中状态" clearable>
          <el-option label="选中" value="1"></el-option>
          <el-option label="未选中" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['action:rule:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['action:rule:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['action:rule:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['action:rule:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-s-data"
          size="mini"
          @click="openSetOrder"
        >焊接顺序优先级设置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ruleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
<!--      <el-table-column label="部门ID" align="center" prop="deptId" />-->
      <el-table-column label="焊接顺序规则" align="center" prop="weldingSequenceRule" />
      <el-table-column label="焊接顺序优先级" align="center" prop="weldingSequencePriority" />
      <el-table-column label="选中状态" align="center" prop="selectedState"  >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.selectedState=='1'" type="success">选中</el-tag>
          <el-tag v-else type="danger">未选中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['actionn:rule:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['actionn:rule:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改动作规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="部门ID" prop="deptId">-->
<!--          <el-input v-model="form.deptId" placeholder="请输入部门ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="焊接规则" prop="weldingSequenceRule">
          <el-input v-model="form.weldingSequenceRule" placeholder="请输入焊接顺序规则" />
        </el-form-item>
        <el-form-item label="优先级" prop="weldingSequencePriority">
<!--          <el-input v-model="form.weldingSequencePriority" placeholder="请输入焊接顺序优先级" />-->
          <el-select v-model="form.weldingSequencePriority" placeholder="请选择焊接顺序优先级" clearable>
            <el-option label="高" value="高"></el-option>
            <el-option label="中" value="中"></el-option>
            <el-option label="低" value="低"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选中状态" prop="selectedState">
<!--          <el-input v-model="form.selectedState" placeholder="请输入选中状态" />-->
          <el-switch
            v-model="form.selectedState"
            :active-value=1
            :inactive-value=0
            disabled
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="焊接顺序优先级设置"  style="align-content: center;" :visible.sync="dialogFormVisible" width="500px" append-to-body>
      <el-form :model="form1" label-width="80px">
        <el-form-item label="高级" >
          <el-radio-group v-model="form1.gao">
            <el-radio v-for="item in gaos"
                      :label="item.id"
                      :value="item.selectedState"
                      :key="item.id">{{item.weldingSequenceRule}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="中级" >
          <el-radio-group v-model="form1.zhong">
            <el-radio v-for="item in zhongs"
                      :label="item.id"
                      :value="item.selectedState"
                      :key="item.id">{{item.weldingSequenceRule}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="低级" >
          <el-radio-group v-model="form1.di">
            <el-radio v-for="item in dis"
                      :label="item.id"
                      :value="item.selectedState"
                      :key="item.id">{{item.weldingSequenceRule}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="checkSet">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listRule, getRule, delRule, addRule, updateRule, updateRuleState } from '@/api/zndd/action/rule'

export default {
  name: "Rule",
  data() {
    return {
      gaos:[],
      zhongs:[],
      dis:[],
      form1:{},
      dialogFormVisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 动作规则表格数据
      ruleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        weldingSequenceRule: null,
        weldingSequencePriority: null,
        selectedState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询动作规则列表 */
    getList() {
      this.loading = true;
      listRule(this.queryParams).then(response => {
        this.ruleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        weldingSequenceRule: null,
        weldingSequencePriority: null,
        selectedState: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加动作规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRule(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改动作规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(row.selectedState==1){
        this.$modal.msgError("该动作规则已启用，不能删除！如需删除请先修改顺序优先级");
      }else{
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除动作规则编号为"' + ids + '"的数据项？').then(function() {
          return delRule(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('action/rule/export', {
        ...this.queryParams
      }, `动作规则_${new Date().getTime()}.xlsx`)
    }, /** 序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if (pageNum !== -1 && pageNum !== 0) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return index + 1;
      }
    },openSetOrder(){
      listRule(this.queryParams).then(response => {
        this.gaos = response.rows.filter(item => item.weldingSequencePriority == "高");
        this.zhongs = response.rows.filter(item => item.weldingSequencePriority == "中");
        this.dis = response.rows.filter(item => item.weldingSequencePriority == "低");
        // // 设置回显值
        // this.form1.gao = this.gaos.find(item => item.selectedState === 1)?.id;
        // this.form1.zhong = this.zhongs.find(item => item.selectedState === 1)?.id;
        // this.form1.di = this.dis.find(item => item.selectedState === 1)?.id;
      })
        this.dialogFormVisible=true;
    },checkSet(){
      var ids=[this.form1.gao,this.form1.zhong,this.form1.di];
      updateRuleState(ids).then(response => {
        this.$modal.msgSuccess("设置成功");
        this.getList();
      })
      this.dialogFormVisible=false;
    },
  }
};
</script>

<style>

</style>
